package com.hhh.demo.sx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhh.demo.sx.bean.Role;
import com.hhh.demo.sx.bean.User;
import com.hhh.demo.sx.bean.UserRole;
import com.hhh.demo.sx.mapper.RoleMapper;
import com.hhh.demo.sx.mapper.UserMapper;
import com.hhh.demo.sx.mapper.UserRoleMapper;
import com.hhh.demo.sx.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhh
 * @since 2021-12-30
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Override
    public Page<Role> findAll(Page<Role> page, QueryWrapper wrapper) {

        return roleMapper.selectPage(page,wrapper);
    }
    @Override
    public Integer doDel(Integer id){//0正常，1删除失败，2有用户有这个权限
        List<UserRole> list = userRoleMapper.selectByRoleId(id);
        if(list==null||list.isEmpty()){
            int ans = roleMapper.deleteById(id);
            return ans>0?0:1;
        }else {
            return 2;
        }
    }


}
